public class J2001 {
    public static int binary(int[] arr, int num){
        int i = 0;
        //最后一个元素的下标
        int j = arr.length - 1;
        boolean m = false;
        int k = 0;
        while( i < j){
            k = (i + j) / 2;
            if(arr[k] == num){
                m = true;
                break;
            }else if(arr[k] > num){
                j = k;
            }else if (arr[k] < num){
                i = k;
            }
        }
        if(m){
            return k;
        }else{
            return -1;
        }
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,3,4,5,6,7,8,9,10};
        int ret = binary(arr,5);
        if(ret != -1){
            System.out.println("找到了，下标为：" + ret);
        }else{
            System.out.println("没找到");
        }
    }
}
